/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AliperLoginWindow.java
 *
 * Created on 2009-11-13, 10:23:45
 */

package com.alibaba.aliper.client.view;

import java.net.MalformedURLException;

import javax.swing.JOptionPane;

import com.alibaba.aliper.client.model.IModelAccess;

/**
 *
 * @author cori-chenxx
 */
public class AliperLoginWindow extends javax.swing.JFrame {

    /** Creates new form AliperLoginWindow */
    public AliperLoginWindow() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextField1Url = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jComboBox1Project = new javax.swing.JComboBox();
        jButton1Update = new javax.swing.JButton();
        jButton2Login = new javax.swing.JButton();
        jButton3Cancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        setTitle("Login To Aliper Server");

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabel1.setText("URL");

        jTextField1Url.setBackground(new java.awt.Color(255, 255, 255));
        jTextField1Url.setText("http://10.20.129.139:8080/aliper-server/AliperServlet");
        jTextField1Url.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jTextField1Url.setPreferredSize(new java.awt.Dimension(67, 28));

        jLabel2.setText("Project");

        jComboBox1Project.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jButton1Update.setText("update");
        jButton1Update.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1UpdateActionPerformed(evt);
            }
        });

        jButton2Login.setText("Login");
        jButton2Login.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2LoginActionPerformed(evt);
            }
        });

        jButton3Cancel.setText("Cancel");
        jButton3Cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3CancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jButton2Login, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton3Cancel, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(12, 12, 12))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBox1Project, 0, 172, Short.MAX_VALUE)
                        .addGap(13, 13, 13)
                        .addComponent(jButton1Update)
                        .addContainerGap())
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(30, 30, 30)
                        .addComponent(jTextField1Url, javax.swing.GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE)
                        .addContainerGap())))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(26, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1Url, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox1Project, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1Update)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2Login)
                    .addComponent(jButton3Cancel))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(17, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>



    // Variables declaration - do not modify
    private javax.swing.JButton jButton1Update;
    private javax.swing.JButton jButton2Login;
    private javax.swing.JButton jButton3Cancel;
    private javax.swing.JComboBox jComboBox1Project;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextField1Url;
    // End of variables declaration

    
    
    private void jButton1UpdateActionPerformed(java.awt.event.ActionEvent evt) {
        String[] projects = null;
    	try {
    		projects = this.model.getProjects(this.jTextField1Url.getText());
		} catch (Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(this, "can not connect to " + this.jTextField1Url.getText());
			return ;
		}
		this.updateProjectComBox(projects);
    }

    private void updateProjectComBox(String[] projects) {
    	   jComboBox1Project.setModel(new javax.swing.DefaultComboBoxModel(projects));
	}

	private void jButton2LoginActionPerformed(java.awt.event.ActionEvent evt) {
        this.setVisible(false);
        if(this.model != null){
        	this.model.setServiceUrl(this.jTextField1Url.getText());
        	Object selectedItem = this.jComboBox1Project.getSelectedItem();
        	if(selectedItem == null){
        		JOptionPane.showMessageDialog(this, "未选择项目","出错了", JOptionPane.ERROR_MESSAGE);
        		this.setVisible(true);
        		return;
        	}
        	this.model.setProject(selectedItem.toString());
        	try {
				this.model.connect();
			} catch (Exception e) {
				e.printStackTrace();
				this.setVisible(true);
			}
        }
    }

    private void jButton3CancelActionPerformed(java.awt.event.ActionEvent evt) {
        this.setVisible(false);
    }
    
    private IModelAccess model = null;
    public void setModel(IModelAccess model){
    	this.model = model;
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AliperLoginWindow().setVisible(true);
            }
        });
    }
}
